package com.cuckoo.mall.thus.menuaccount.login;

import com.bgn.baseframe.base.BasePresenterImpl;
import com.bgn.baseframe.network.HttpObserver;
import com.bgn.baseframe.network.bean.TDataBean;
import com.bgn.baseframe.utils.ASEUtils;
import com.cuckoo.mall.thus.manager.UserInfoManager;
import com.cuckoo.mall.javabean.UserInfo;
import com.cuckoo.mall.network.ApiManager;


/** * 作者：wl on 2018/4/ * 邮箱：wanglun@stosz.com */

public class LoginPresenter extends BasePresenterImpl<LoginContract.View> implements LoginContract.Presenter {

    @Override
    public void userLogin(final String mail, final String pwd) {
        final String aseKey = ASEUtils.getKey();
        ApiManager.login(mail, pwd, aseKey).subscribe(new HttpObserver<TDataBean<UserInfo>>(mView) {
            @Override
            public void onSucceed(TDataBean<UserInfo> value) {
                String encodeToken = value.getData().getToken();
                String token = ASEUtils.Decrypt(encodeToken, aseKey);
                value.getData().setToken(token);
                if (value.getData().getRealtoken().equals(token)) {
                    mView.onLoginSucceed();
                    UserInfoManager.getInstance().loginSucceed(value.getData());
                }
            }

            @Override
            public void onDefeat(TDataBean<UserInfo> value) {
                super.onDefeat(value);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });

    }

    @Override
    public void userRegister(String mail, String pwd) {
        final String aseKey = ASEUtils.getKey();
        ApiManager.register(mail, pwd, aseKey).subscribe(new HttpObserver<TDataBean<UserInfo>>(mView) {
            @Override
            public void onSucceed(TDataBean<UserInfo> value) {
                String encodeToken = value.getData().getToken();
                String token = ASEUtils.Decrypt(encodeToken, aseKey);
                value.getData().setToken(token);
                if (value.getData().getRealtoken().equals(token)) {
                    mView.onRegisterSucceed();
                    UserInfoManager.getInstance().loginSucceed(value.getData());
                }
            }

            @Override
            public void onDefeat(TDataBean<UserInfo> value) {
                super.onDefeat(value);

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);

            }
        });
    }

    @Override
    public void thridPartLogin(String facebook_id, String email, String phone, String third_party_id, String nickname, String head_img) {
        final String aseKey = ASEUtils.getKey();
        ApiManager.thridPartLogin(facebook_id, email, phone, third_party_id, nickname, head_img, aseKey).subscribe(new HttpObserver<TDataBean<UserInfo>>(mView) {
            @Override
            public void onSucceed(TDataBean<UserInfo> value) {
                String encodeToken = value.getData().getToken();
                String token = ASEUtils.Decrypt(encodeToken, aseKey);
                value.getData().setToken(token);
                if (value.getData().getRealtoken().equals(token)) {
                    mView.onLoginSucceed();
                    UserInfoManager.getInstance().loginSucceed(value.getData());
                }
            }

            @Override
            public void onDefeat(TDataBean<UserInfo> value) {
                super.onDefeat(value);

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

}
